package com.good.util;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import javax.imageio.ImageIO;

import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.geometry.Positions;

/**
 * 
 * @author iii Thumbnails 图片工具类
 */
public class ImageUtile {
	private ImageUtile() {

	}

	public static void main(String[] args) {
		InputStream in=null;
		try {
			in = new FileInputStream("F:"+File.separator+"Chrysanthemum.jpg");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		String path="F:"+File.separator+"out.jpg";
		//setSize(in,path,2000,2000);
		//setScale(in,path,0.5);
		//setNoScale(in,path,200,100);
		//ImageUtile.setTransform(in,path,-30,300,300);
		//ImageUtile.setWatermark(in, path, Positions.BOTTOM_RIGHT, "F:"+File.separator+"Chrysanthemum.jpg", (float) 0.5, 1000,1000);
		//ImageUtile.setCut(in, path, Positions.TOP_LEFT, 300, 300, 300, 300);
		//ImageUtile.setCut(in, path, 0, 0, 100, 100, 100, 100);
		ImageUtile.changeStyle(in, path, "png", 100, 100);
	}

	/**
	 * 
	 * @param in
	 *            图片输入流
	 * @param path
	 *            输出地址
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @return
	 */
	public static boolean setSize(InputStream in, String path, int width,
			int height) {
		try {
			Thumbnails.of(in).size(width, height).toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 图片缩放
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param scale
	 *            缩放比例
	 * @return
	 */
	public static boolean setScale(InputStream in, String path, double scale) {
		try {
			Thumbnails.of(in).scale(scale).toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 不按比例缩放
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @return
	 */
	public static boolean setNoScale(InputStream in, String path, int width,
			int height) {
		// keepAspectRatio(false) 默认是按照比例缩放的
		try {
			Thumbnails.of(in).size(width, height).keepAspectRatio(false)
					.toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 角度
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param angle
	 *            角度 30 60 90 -xxx等
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @return
	 */
	public static boolean setTransform(InputStream in, String path,
			double angle, int width, int height) {
		try {
			Thumbnails.of(in).size(width, height).rotate(angle).toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 水印图
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param position
	 *            水印在图片的位置 (Positions.BOTTOM_RIGHT)
	 * @param waterpath
	 *            水印图位置
	 * @param opacity
	 *            透明度
	 * @param width
	 *            宽度
	 * @param height
	 *            高度
	 * @return
	 */
	public static boolean setWatermark(InputStream in, String path,
			Positions position, String waterpath, float opacity, int width,
			int height) {
		// watermark(位置，水印图，透明度)
		try {
			Thumbnails
					.of(in)
					.size(width, height)
					.watermark(position, ImageIO.read(new File(waterpath)),
							opacity).outputQuality(1.0f).toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 剪切图片
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出path
	 * @param position
	 *            位置(Positions.BOTTOM_RIGHT)
	 * @param picWidth
	 *            剪切图片宽
	 * @param picHeight
	 *            剪切图片高
	 * @param width
	 *            输出图片宽
	 * @param height
	 *            输出图片高
	 * @return
	 */
	public static boolean setCut(InputStream in, String path,
			Positions position, int picWidth, int picHeight, int width,
			int height) {
		try {
			Thumbnails.of(in).sourceRegion(position, picWidth, picHeight)
					.size(width, height).keepAspectRatio(false).toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 指定位置剪切
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param x
	 *            x轴
	 * @param y
	 *            y轴
	 * @param picWidth
	 *            图片大小
	 * @param picHeight
	 *            图片大小
	 * @param width
	 *            输出图片宽
	 * @param height
	 *            输出图片高
	 * @return
	 */
	public static boolean setCut(InputStream in, String path, int x, int y,
			int picWidth, int picHeight, int width, int height) {
		try {
			Thumbnails.of(in).sourceRegion(x, y, picWidth, picHeight)
					.size(width, height).keepAspectRatio(false).toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 转换文件格式
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param style
	 *            输出格式(jpg png)
	 * @param width
	 *            宽度
	 * @param heigth
	 *            高度
	 * @return
	 */
	public static boolean changeStyle(InputStream in, String path,
			String style, int width, int heigth) {
		try {
			Thumbnails.of(in).size(width, heigth).outputFormat(style)
					.toFile(path);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 转到输出流
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出流path
	 * @param width
	 *            宽度
	 * @param heigth
	 *            高度
	 * @return
	 */
	public static boolean toOutput(InputStream in, String path, int width,
			int heigth) {
		// toOutputStream(流对象)
		try {
			OutputStream os = new FileOutputStream(path);
			Thumbnails.of(in).size(width, heigth).toOutputStream(os);
			return true;
		} catch (IOException e) {
			return false;
		}
	}

	/**
	 * 输出到BufferedImage
	 * 
	 * @param in
	 *            输入流
	 * @param path
	 *            输出地址
	 * @param style
	 *            图片格式
	 * @param width
	 *            图片宽度
	 * @param heigth
	 *            图片高度
	 * @return
	 */
	public static boolean toImage(InputStream in, String path, String style,
			int width, int heigth) {
		// asBufferedImage() 返回BufferedImage
		BufferedImage thumbnail = null;
		try {
			thumbnail = Thumbnails.of(in).size(width, heigth).asBufferedImage();
			ImageIO.write(thumbnail, style, new File(path));
			return true;
		} catch (IOException e) {
			return false;
		}
	}
}
